'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            if($state.includes('**.module.update')){
                title="编辑模块信息";
                var moduleNo = $state.params.id;
                activate(moduleNo);
                $scope.isDisabled = false;
            }else if($state.includes('**.module.create')){
                title="添加模块";
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                // if(!$('#moduleNo').val()){
                //     $scope.alertModal({'content':'模块编号不能为空'});
                //     return;
                // }

                if(!$('#moduleName').val()){
                    $scope.alertModal({'content':'模块名称不能为空'});
                    return;
                }

                if(!$('#sortNum').val()){
                    $scope.alertModal({'content':'排序号不能为空'});
                    return;
                }

                if(!$('#enable').val()){
                    $scope.alertModal({'content':'是否可用不能为空'});
                    return;
                }
                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.moduleNo ? $scope.app.project_name+'/messageSets/module/update' : $scope.app.project_name+'/messageSets/module/add',
                        // url : $scope.app.project_name+'/messageSets/module/update',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.messageSets.module.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function activate(moduleNo) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/messageSets/module/detail',
                    data: {'moduleNo': moduleNo}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);